package com.lkm.app_ui.m.Imple.goods;

import android.os.Handler;
import android.os.Looper;

import com.lkm.app_ui.AppConst;
import com.lkm.app_ui.RouterUINavigation;
import com.lkm.app_ui.RunTimeData;
import com.lkm.app_ui.help.AppUIHelp;
import com.lkm.app_ui.m.goods.BarcodeResultBO;
import com.lkm.app_ui.m.goods.GoodsEntity;
import com.lkm.app_ui.m.goods.GoodsModule;
import com.lkm.app_ui.m.goods.OnBarcodeResultEvent;
import com.lkm.app_ui.m.input.OnInputContentEvent;
import com.lkm.frame.entity.ResponEntityAbs;
import com.lkm.frame.otto.LinkBus;
import com.lkm.frame.otto.Observer;
import com.lkm.frame.task.ProgressData;
import com.lkm.frame.task2.BackCallAdapter;
import com.lkm.frame.task2.Task;
import com.lkm.javatool.CollectionHelp;
import com.lkm.javatool.StringHelp;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



class GoodsModuleImpl implements GoodsModule {

    
    private GoodsBO goodsBO;

    public GoodsModuleImpl() {
        
        HoldGoodsInfoTask.start(true).addBackCall(new BackCallAdapter<GoodsBO, ResponEntityAbs<GoodsBO>>() {
            @Override
            public void onFinish(Task<?, GoodsBO, ResponEntityAbs<GoodsBO>> task, ResponEntityAbs<GoodsBO> re) {
                super.onFinish(task, re);
                if (re.isSuccess()) {
                    goodsBO = re.getData();
                }
            }

            @Override
            public void onProgresing(Task<?, GoodsBO, ResponEntityAbs<GoodsBO>> task, ProgressData<GoodsBO> progressData) {
                super.onProgresing(task, progressData);
                goodsBO = progressData.getData();

            }
        });
        onInputContentEvent();
    }

    
    private void onInputContentEvent() {
        LinkBus.registerReceiver(new Observer<OnInputContentEvent>() {
            
            @Override
            public void onReceiver(OnInputContentEvent data, Runnable nextRun) {
                // json被拦截在：com.lkm.app_ui.m.Imple.fun.InterceptScanInputJson.onInputContentEvent
                BarcodeResultBO barcodeResultBO = barcodeResult(data.getContent());
                if (barcodeResultBO != null) {
                    LinkBus.sendBroadcast(new OnBarcodeResultEvent(barcodeResultBO));
                    AppConst.mainHandler.post(new Runnable() {
                        @Override
                        public void run() {
                            RunTimeData.barcodeResultBO = barcodeResultBO;
                            if (RunTimeData.barcodeResultBO != null) {
                                RouterUINavigation.getInstance().dismissAll();
                                
                                if(barcodeResultBO.goodsEntity==null){
                                    if(goodsBO==null){
                                        AppUIHelp.tryShowTipsLong("本地无商品库，请手动加载");
                                    }else {
                                        AppUIHelp.tryShowTipsLong("无对应的商品，本地商品数量："+getGoodsSize());
                                    }
                                } else if (RunTimeData.barcodeResultBO.goodsEntity != null && RunTimeData.barcodeResultBO.goodsEntity.type == GoodsEntity.Type_countNum) {
                                    RouterUINavigation.getInstance().showCountNum(true);
                                } else {
                                    RouterUINavigation.getInstance().showBarcodeResult();
                                }
                            }
                        }
                    });

                }
            }

            @Override
            public Class getBusEventClass() {
                return OnInputContentEvent.class;
            }
        });
    }

    public static void main(String[] args) {
        Pattern pattern = Pattern.compile("([0-9]{5})([0-9]{6})");
        Matcher matcher = pattern.matcher("00000043123456");
        matcher.find();
        System.out.println(matcher.group());
        System.out.println(matcher.group(1));
        System.out.println(matcher.group(2));
    }

    @Override
    public int getGoodsSize() {
        GoodsBO goodsBo = goodsBO;
        return goodsBo == null ? 0 : CollectionHelp.getSize(goodsBo.goodsEntities);
    }

    

    public BarcodeResultBO findBarcodeResult(String barcode) {
        barcode = barcode.trim();
        GoodsBO goodsBo = goodsBO;

        if (goodsBo == null || CollectionHelp.isEmpty(goodsBo.goodsEntities))
            return new BarcodeResultBO();

        BarcodeResultBO barcodeResultBO = new BarcodeResultBO();
        barcodeResultBO.goodsCode = "";
        barcodeResultBO.price = "";
        barcodeResultBO.goodsEntities = new ArrayList<>();

        int size = goodsBo.goodsEntities.size();
        GoodsEntity goodsEntity;
        for (int i = 0; i < size; i++) {
            goodsEntity = goodsBo.goodsEntities.get(i);
            if (goodsEntity.code != null && goodsEntity.code.equals(barcode)) {
                barcodeResultBO.goodsEntity = goodsEntity;
                break;

            }
            if (goodsEntity.code != null && goodsEntity.code.contains(barcode)) {
                if (barcodeResultBO.goodsEntities.size()<=10){
                    barcodeResultBO.goodsEntities.add(goodsEntity);
                }
            }

        }

        return barcodeResultBO;
    }
    

    private BarcodeResultBO barcodeResult(String barcode) {
        barcode = barcode.trim();

        GoodsBO goodsBo = goodsBO;

        if (goodsBo == null || CollectionHelp.isEmpty(goodsBo.goodsEntities))
            return new BarcodeResultBO();

        if (goodsBo.rulePattern == null || goodsBo.ruleVarIndex == null) {
            
            return new BarcodeResultBO();
        }

        Pattern pattern = goodsBo.rulePattern;
        Matcher matcher = pattern.matcher(barcode);
        matcher.find();


        String goodsCode = "";
        try {
            goodsCode = matcher.group(goodsBo.ruleVarIndex.code);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String price = "";
        try {
            price = matcher.group(goodsBo.ruleVarIndex.price);
            if (price.length() > 2) {
                price = price.substring(0, price.length() - 2) + "." + price.substring(price.length() - 2, price.length());
            } else {
                if (price.length() == 1)
                    price = "0.0" + price;
                else price = "0." + price;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        BarcodeResultBO barcodeResultBO = new BarcodeResultBO();
        barcodeResultBO.goodsCode = goodsCode;
        barcodeResultBO.price = price;

        if (!StringHelp.isEmpty(goodsCode)) {
            int size = goodsBo.goodsEntities.size();
            GoodsEntity goodsEntity;
            for (int i = 0; i < size; i++) {
                goodsEntity = goodsBo.goodsEntities.get(i);
                if (goodsEntity.code != null && goodsEntity.code.equals(goodsCode)) {
                    barcodeResultBO.goodsEntity = goodsEntity;
                    break;
                }

            }

        }
        return barcodeResultBO;
    }

}
